import 'package:enterprise_v6_app/app/components/custom_modele/custom_audioplayers.dart';
import 'package:enterprise_v6_app/app/components/dialog/custom_tape_sheet.dart';
import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/common/simple_choice_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/inspec/ins_task_detail_entity.dart';

class ResponsibilityTaskDetailsController extends GetxController {
  /// 任务id
  String taskId = Get.arguments as String;

  ///排查结果
  List<SimpleChoiceBean> choiceDataList = [];

  /// 执行任务时传递的参数
  InsTaskDetailEntity? detailEntity;

  /// 任务名称
  String get taskName => detailEntity?.riskUnitName ?? '空';

  /// 排查结果是否正常 默认为true
  bool isNormal = true;

  /// 录音key
  final GlobalKey<CustomAudioplayersState> globalKey = GlobalKey();

  /// 是否允许编辑
  bool isEnabled = false;

  @override
  void onInit() {
    super.onInit();
    // choiceDataList.add(SimpleChoiceBean(id: '1', name: '正常'));
    // choiceDataList.add(SimpleChoiceBean(id: '0', name: '存在隐患', isChecked: true));
    // 获取任务详情
    getTaskRecordDetail();
  }

  /// 获取任务详情
  Future<void> getTaskRecordDetail() async {
    final response = await HttpUtil().generateGetRequest<InsTaskDetailEntity>(Interface.getTaskListByHazardId, queryParameters: {
      'taskId': taskId,
    }).execute();

    response.onSuccess((result) {
      if (result.data != null) {
        detailEntity = result.data;
        // 为未排查状态
        if (detailEntity!.checkStatus != null && detailEntity!.checkStatus == 4) {
          isEnabled = true;
        }
        // 为正常时
        if (detailEntity!.checkStatus != null && detailEntity!.checkStatus == 1) {
          choiceDataList.add(SimpleChoiceBean(id: '1', name: '正常'));
          choiceDataList.add(SimpleChoiceBean(id: '0', name: '存在隐患', isChecked: true));
          isNormal = false;
        } else {
          choiceDataList.add(SimpleChoiceBean(id: '1', name: '正常', isChecked: true));
          choiceDataList.add(SimpleChoiceBean(id: '0', name: '存在隐患'));
        }
        update();
      }
    });
  }

  /// 录音
  void onTapTape() {
    CustomTapeSheet(
      onCallBack: (url) {
        detailEntity!.patrolVoices!.add(url);
        globalKey.currentState?.initData();
        update();
      },
    ).showBottomSheetDialog(isDismissible: false);
  }

  /// 取消按钮
  void cancelTap() {
    Get.back();
  }

  /// 提交按钮
  Future<void> submitTap() async {
    if (detailEntity!.inspectionDescription!.isEmpty) {
      AppToast.showWarning('请输入巡检描述!');
      return;
    }
    if (detailEntity!.inspectionPhotos!.isEmpty) {
      AppToast.showWarning('请输入上传拍摄照片!');
      return;
    }
    // 正常
    if (isNormal) {
      // 修改状态为正常
      detailEntity!.checkStatus = 0;
    } else {
      // 异常
      detailEntity!.checkStatus = 1;
    }
    final res = await HttpUtil()
        .generatePostRequest(
          Interface.insExecuteTask,
          data: detailEntity!.toJson(),
        )
        .execute();
    res.onSuccess((result) {
      Get.back(result: 'success');
      AppToast.showSuccess('提交成功!');
    });
  }
}
